//
// Created by 紫霄 on 2020/4/19.
//

#ifndef LEETCODE_30_H
#define LEETCODE_30_H
#include <vector>
using namespace std;
class MinStack {
public:
  /** initialize your data structure here. */
  int size = 0;
  vector<int> m;
  int minVal = INT_MAX;
  vector<int> minOld;
  MinStack() {}

  void push(int x) {
    m.push_back(x);
    ++size;
    if (x <= minVal) {
      minOld.push_back(minVal);
      minVal = x;
    }
  }

  void pop() {
    if (!m.empty()) {
      if (m[m.size() - 1] == minVal) {
        minVal = minOld[minOld.size() - 1];
        minOld.erase(minOld.end() - 1);
      }
      m.erase(m.end() - 1);
    }
  }

  int top() {
    if (!m.empty()) {
      return m[m.size() - 1];
    }
    return m[m.size() - 1];
  }

  int min() { return minVal; }
};
#endif // LEETCODE_30_H
